<?php
require_once ("C_krn_exception.php");

// Funciones net
class c_krn_audit {
	
	// Evitamos el clonaje del objeto. Patrón Singleton
	private function __clone() {
	}
	private function __wakeup() {
	}
	private static $_instance;
	public static function getInstance() {
		if (! (self::$_instance instanceof self)) {
			self::$_instance = new self ();
		}
		return self::$_instance;
	}
	
	// Variables de Error CORRELATIVA
	private $SEQ_ERROR;
	public function getSEQ_ERROR() {
		return $this->SEQ_ERROR;
	}
	public function setSEQ_ERROR($nERROR) {
		$this->SEQ_ERROR = $this->SEQ_ERROR + $nERROR;
	}
	
	/*
	 * Add an audit to the log Some Constants Used $GLOBALS["CODEX"] is an array of short descriptions for each audit type so that the log reads better. use and AND statement to compare the type of information you are wanting to audit and the level that you have told your program to record type, level AUDIT_NORMAL & AUDIT_LEVEL = 00000 ie 00001 & 11110 === 00000 ( Do not log ) AUDIT_DB_UPDATE & AUDIT_LEVEL = 00100 ie 00100 & 11110 === 00100 ( Add to log ) AUDIT_DB_INSERT & AUDIT_LEVEL = 00010 ie 00010 & 11110 === 00010 ( Add to log ) So when you AND a interger with a MASK in this case the AUDIT_LEVEL @access public @author IrishAdo <irishado@hotmail.com> @param String Class you were in @param String Type of Audit information @param String Message
	 */
	public function AddAudit($typeof, $pDIR, $pFILE, $pLINE, $pCLASS, $pMETHOD, $pFUNCTION, $pLOG) {
		global $IDIOMA, $language;
		
		if ((AUDIT_LEVEL & $typeof) * 1 === $typeof && AUDIT_LEVEL != AUDIT_NONE) {
			
			$file = LOG_NAME;
			$path = APPLICATION_DIR_LOG;
			
			$path = $path . $file;
			
			$fp = fopen ( $path, "a" );
			if ($fp) {
				
				if ($typeof == AUDIT_DB_SQL) {
					fwrite ( $fp, str_pad ( " ", 12, ' ', STR_PAD_RIGHT ) );
					fwrite ( $fp, " [" . str_pad ( "SQL", 7, " ", STR_PAD_RIGHT ) . "] " . $pLOG . "\r\n" );
				} else if ($typeof == AUDIT_DB_SQL_FETCH) {
					fwrite ( $fp, str_pad ( " ", 2, ' ', STR_PAD_RIGHT ) );
					fwrite ( $fp, " [" . str_pad ( " FETCH ", 7, " ", STR_PAD_RIGHT ) . "] " . $pLOG . "\r\n" );
				} else {
					// Si está definido el id de usuario lo ponemos en la auditoria para rastrear quien hace que
					$SESSION_ID = isset ( $_SESSION ['SESSION_ID'] ) ? $_SESSION ['SESSION_ID'] : $language [$IDIOMA] [45];
					
					// Para ver o no la hora en el log
					if (AUDIT_SHOW_LOG_DATE) {
						  fwrite ( $fp, str_pad ( date ( "d/m/Y - H:i:s", time () ), 21, " ", STR_PAD_LEFT ) . ' ' . $SESSION_ID ); 
					}
					// Para ver más datos de los necesarios
					if (AUDIT_SHOW_EXPAND_LOG) {
					  fwrite ( $fp, str_pad ( $this->getSEQ_ERROR (), 6, '0', STR_PAD_LEFT ) . " " . str_pad ( ' ', 6, ' ', STR_PAD_LEFT ) . str_pad ( $pDIR, 25, ' ', STR_PAD_RIGHT ) . "/" . str_pad ( basename ( $pFILE ), 25, ' ', STR_PAD_RIGHT ) . " (" . str_pad ( $pLINE, 5, " ", STR_PAD_LEFT ) . ") " . str_pad ( $pMETHOD, 30, " ", STR_PAD_LEFT ) . " " . str_pad ( date ( "d/m/Y - H:i:s", time () ), 21, " ", STR_PAD_LEFT ) . str_pad ( $SESSION_ID, 33, " ", STR_PAD_LEFT ) );
					  fwrite ( $fp, "\r\n" );
					  fwrite ( $fp, str_pad ( " ", 12, ' ', STR_PAD_RIGHT ) );
					}
					
					if ($typeof == AUDIT_DB) {
						fwrite ( $fp, " [" . str_pad ( "DB", 7, " ", STR_PAD_RIGHT ) . "] " . $pLOG . "\r\n" );
					} else
						fwrite ( $fp, " [" . str_pad ( "APP", 7, " ", STR_PAD_RIGHT ) . "] " . $pLOG . "\r\n" );
					$this->setSEQ_ERROR ( 1 );
				}
				fclose ( $fp );
			} else {
				print "Imposible escribir log en: $path.";
				exit ();
			}
		}
	}
	function __construct() {
		$this->AddAudit ( AUDIT_EXTEND, __DIR__, __FILE__, __LINE__, __CLASS__, __METHOD__, __FUNCTION__, "::constructor (" . __CLASS__ . ")" );
	}
}
;

// Instancia de la clase
try {
	// Instancia de la clase
	$cAUDIT = c_krn_audit::getInstance ();
} catch ( Exception $e ) {
	global $IDIOMA, $language;
	echo $language [$IDIOMA] [59] . $e->getMessage ();
}

?>
